package com.weikuang.fareast.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.trello.rxlifecycle2.android.ActivityEvent;
import com.weikuang.fareast.R;
import com.weikuang.fareast.adapter.WashingAddAdapter;
import com.weikuang.fareast.bean.DeliveredOfficeBean;
import com.weikuang.fareast.bean.DeliveredOfficeDetailBean;
import com.weikuang.fareast.bean.DeliveredOrderBean;
import com.weikuang.fareast.bean.HotelAddBean;
import com.weikuang.fareast.bean.SimpleBean;
import com.weikuang.fareast.http.MainHttp;
import com.weikuang.fareast.utils.UserHelper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dlc.commonlibrary.okgo.rx.OkObserver;
import cn.dlc.commonlibrary.ui.widget.CommonItemView;
import cn.dlc.commonlibrary.ui.widget.TitleBar;
import cn.dlc.commonlibrary.ui.widget.popup.CommonPopupList;
import cn.dlc.commonlibrary.utils.ToastUtil;
import cn.dlc.commonlibrary.utils.rv_tool.RecyclerViewUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

//洗涤中心入库  医院送货单

public class HotelAddActivity extends BaseActivity {
    @BindView(R.id.mTitleBar)
    TitleBar mTitleBar;
    @BindView(R.id.tv_hospital)
    TextView tvHospital;
    @BindView(R.id.drop_hospital)
    FrameLayout dropHospital;
    @BindView(R.id.item_hospital)
    CommonItemView itemHospital;
    @BindView(R.id.tv_offic)
    TextView tvOffic;
    @BindView(R.id.drop_offic)
    FrameLayout dropOffic;
    @BindView(R.id.item_offic)
    CommonItemView itemOffic;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.btn_read)
    Button btnRead;

    private CommonPopupList mOfficPopup;
    private List<DeliveredOfficeBean.DataBean> mOfficList;
    String offic_id = "",order_id = "";

    private CommonPopupList mOrderPopup;
    private List<DeliveredOrderBean.DataBean> mOrderList;

    WashingAddAdapter mWashingAddAdapter;
    List<HotelAddBean> datasList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_washing_add;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_washing_add);
        ButterKnife.bind(this);
        String mhotel_name = UserHelper.get().gethotel()+"配送中心布草入库";
        mTitleBar.setTitle(mhotel_name);
        initView();
        InitRecyclerView();
        getorder();
    }

    @OnClick({R.id.drop_hospital, R.id.drop_offic, R.id.btn_read})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.drop_hospital:
                mOrderPopup.showAsDropDown(view);
                break;
            case R.id.drop_offic:
                mOfficPopup.showAsDropDown(view);
                break;
            case R.id.btn_read:
                stockCommit();
                break;
        }
    }
    private void initView() {
        mOrderPopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(dropHospital)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                        tvHospital.setText(item);
                        if (mOrderList != null) {
                            order_id = mOrderList.get(position).id;
                            deliveredDetail(order_id);
                        }

                    }
                })
                .build();
        mOfficPopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(dropOffic)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                        tvOffic.setText(item);
                        if (mOfficList != null) {
                            offic_id = mOfficList.get(position).id;
                            //deliveredOfficeDetail(order_id,offic_id);
                        }

                    }
                })
                .build();

    }

    private void InitRecyclerView() {
        mWashingAddAdapter = new WashingAddAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        RecyclerViewUtil.setDefaultDivider(mRecyclerView, linearLayoutManager);
        mRecyclerView.setAdapter(mWashingAddAdapter);

    }

    private void getorder() {
        MainHttp.get().deliveredOrder()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<DeliveredOrderBean>() {
                    @Override
                    public void onSuccess(DeliveredOrderBean hotelBean) {
                        showOneToast(hotelBean.msg);
                        List<String> datas = new ArrayList<>();

                        if (hotelBean.data != null &&hotelBean.data.size() == 0){
                            ToastUtil.show(getActivity(),"数据为空");
                        }
                        mOrderList = hotelBean.data;
                        for (DeliveredOrderBean.DataBean bean : hotelBean.data) {
                            datas.add(bean.orderno);
                        }
                        mOrderPopup.setData(datas);
                    }

                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(getActivity(),message);
                    }
                });
    }
    private void getOffice(String order_id) {
        MainHttp.get().deliveredOffice(order_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<DeliveredOfficeBean>() {
                    @Override
                    public void onSuccess(DeliveredOfficeBean officeBean) {
                        List<String> datas = new ArrayList<>();
                        mOfficList = officeBean.data;
                        for (DeliveredOfficeBean.DataBean bean : officeBean.data) {
                            datas.add(bean.title);
                        }
                        mOfficPopup.setData(datas);
                    }
                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(getActivity(),message);
                    }
                });
    }

    private void deliveredDetail(String orderId) {
        MainHttp.get().deliveredDetail(orderId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<DeliveredOfficeDetailBean>() {
                    @Override
                    public void onSuccess(DeliveredOfficeDetailBean officeBean) {
                        showOneToast(officeBean.msg);

                        datasList.clear();
                        for (int i = 0; i < officeBean.data.detail.size();i++) {
                            HotelAddBean bean1 = new HotelAddBean(officeBean.data.hotel.title,officeBean.data.office.get(0).title,officeBean.data.detail.get(i).title,
                                    officeBean.data.detail.get(i).goods_id, Integer.valueOf(officeBean.data.detail.get(i).amount),Integer.valueOf(officeBean.data.detail.get(i).amount));
                            datasList.add(bean1);
                        }
                        mWashingAddAdapter.setNewData(datasList);

                    }

                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(getActivity(),message);
                    }
                });
    }

    private void stockCommit(){
        String goods_id = "",  nums = "";
        clearNums();
        for (int i = 0; i < datasList.size(); i++) {
            goods_id = goods_id + datasList.get(i).id + ",";
        }
        if (!TextUtils.isEmpty(goods_id)) {
            goods_id = goods_id.substring(0, goods_id.length() - 1);
        } else {
            goods_id = "";
        }

        for (int i = 0; i < datasList.size(); i++) {
            nums = nums + datasList.get(i).realnum + ",";
        }
        if (!TextUtils.isEmpty(goods_id)) {
            nums = nums.substring(0, nums.length() - 1);
        } else {
            nums = "";
        }
        showWaitingDialog("请等待",true);
        MainHttp.get().stockCommit(order_id,goods_id,offic_id,nums)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<SimpleBean>() {
                    @Override
                    public void onSuccess(SimpleBean bean) {
                        ToastUtil.show(HotelAddActivity.this,bean.msg);
                        dismissWaitingDialog();
                        datasList.clear();
                        mWashingAddAdapter.setNewData(datasList);
                    }
                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(HotelAddActivity.this,message);
                        dismissWaitingDialog();
                    }
                });
    }

    /**
     * 是否清空为0数据
     * @return
     */
    private void clearNums(){
        Iterator<HotelAddBean> ite = datasList.iterator();
        while(ite.hasNext()){
            HotelAddBean str = (HotelAddBean)ite.next();
            if(str.nums == 0){
                ite.remove();
            }
        }
    }
}
